#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/4/9  20:43
# @Author: 余浪人
# @email: yulangren520@gmail.com

from apps import create_app
from apps.models.synthesize_model import TL_Connector_Model, TL_WebApp_Model, TL_Indexes_Model, TL_ThemePush_Model
from apps.models.system_model import TL_System_Model, TL_Redis_Model, TL_PayConf_Model, TL_Email_Model, TL_Save_Model
from apps.models.member_model import TL_Member_Model, db
from apps.lib.public import initialize
cms_app = create_app('apps.setting.ProductionConfig')
if __name__ == '__main__':
    with cms_app.app_context():
        db.create_all()
        if not len(TL_System_Model.query.filter().all()):
            initialize(TL_System_Model, db)
        if not len(TL_Redis_Model.query.filter().all()):
            initialize(TL_Redis_Model, db)
        if not len(TL_PayConf_Model.query.filter().all()):
            initialize(TL_PayConf_Model, db)
        if not len(TL_Connector_Model.query.filter().all()):
            initialize(TL_Connector_Model, db, {"status": 0})
        if not len(TL_WebApp_Model.query.filter().all()):
            initialize(TL_WebApp_Model, db, {"status": 0})
        if not len(TL_Email_Model.query.filter().all()):
            initialize(TL_Email_Model, db)
        if not len(TL_Save_Model.query.filter().all()):
            initialize(TL_Save_Model,db)
        if not len(TL_Indexes_Model.query.filter().all()):
            initialize(TL_Indexes_Model,db)
        if not len(TL_ThemePush_Model.query.filter().all()):
            initialize(TL_ThemePush_Model,db,{'identifier':'default_one','is_current':1})
            initialize(TL_ThemePush_Model,db,{'identifier':'default_two'})
        if not len(TL_Member_Model.query.filter().all()):
            initialize(TL_Member_Model, db, {"username": "root", "password": "root", "remark": "超级管理员"})
    cms_app.run(host="0.0.0.0", port=cms_app.config.get('PORT'), threaded=True)
